home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / actlib13.zip / DATE.ZIP / DAYOFWK.C < prev    next >
Text File  |  1993-01-14  |  2KB  |  64 lines

  1. /*  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)  */
  2.  
  3. #include "date.h"
  4. #include <time.h>
  5.  
  6.  
  7. /***
  8.  *
  9.  *  Function  :    day_of_week
  10.  *
  11.  *  Return    :    0 = sunday, 1 = monday,...
  12.  *
  13.  *  Decisions :    If date is not valid, return -1.
  14.  *
  15.  ***/
  16.  
  17. int day_of_week( int day , int month , int year )
  18.  
  19. { struct tm dtime ;
  20.  
  21.   if ( ! isdatevalid(day , month , year) ) return -1 ;
  22.  
  23.   dtime.tm_year = year - 1900 ;
  24.   dtime.tm_mon  = month - 1 ;
  25.   dtime.tm_mday = day ;
  26.   dtime.tm_hour = 0;
  27.   dtime.tm_min  = 0;
  28.   dtime.tm_sec  = 1;
  29.   dtime.tm_isdst = -1;
  30.  
  31.   /*  call mktime to fill in the weekday field of the structure */
  32.  
  33.   if ( mktime(&dtime) == -1 ) return -1 ;
  34.  
  35.   return dtime.tm_wday ;
  36.   
  37. /*
  38.   const signed char calendar[12] = { 0 , 1 , -1 , 0 , 0 , 1 , 1 , 2 , 3 , 3 , 4 , 4 }
  39.   
  40.   return ( 365L * (year - 1) + (year - 1) / 4 - (year - 1) / 100 - (year - 1) / 400
  41.           + (month - 1) * 30 + calendar[month - 1] 
  42.           + isleapyear(year) && (month > 2)
  43.           + day
  44.          ) % 7 ;
  45. */
  46.  
  47. /*
  48.   const signed char calendar[12] = { 6 , 2 , 2 , 5 , 0 , 3 , 5 , 1 , 4 , 6 , 2 , 4 }  
  49.   int century ;
  50.   long result = 0 ;
  51.   if ( isleapyear(year) && (month < 3) ) result = -1 ;
  52.  
  53.   century = year / 100 ; year %= 100 ;
  54.  
  55.   result += ( century * 5 + century / 4 + year + year / 4 + day + calendar[month] ) % 7 ;
  56.  
  57. We can precompute x = (century * 5 + century / 4 + year + year / 4) % 7
  58. and use result += ( x + day + calendar[month] ) % 7 
  59.  
  60.  
  61. */
  62. }
  63.  
  64.